<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px;">
      <el-breadcrumb-item :to="{ path: '/sys-admin' }">
        <i class="el-icon-s-promotion"></i> 后台管理
      </el-breadcrumb-item>
      <el-breadcrumb-item>添加店铺</el-breadcrumb-item>
    </el-breadcrumb>

    <el-divider></el-divider>

    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="130px" class="demo-ruleForm">
      <el-form-item label="店铺名称" prop="name">
        <el-input v-model="ruleForm.name"></el-input>
      </el-form-item>
      <el-form-item label="店铺地址" prop="address">
        <el-input v-model="ruleForm.address"></el-input>
      </el-form-item>
      <el-form-item label="店铺logo" prop="logo">
        <el-input v-model="ruleForm.logo"></el-input>
      </el-form-item>
      <el-form-item label="商家电话" prop="phone">
        <el-input v-model="ruleForm.phone"></el-input>
      </el-form-item>
      <el-form-item label="商家微信" prop="wechat">
        <el-input v-model="ruleForm.wechat"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">添加</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      ruleForm: {
        name: '龙家海鲜店',
        address: '凤岭路8号',
        logo: '一一一',
        phone: '11234567342',
        wechat:'wx1233444'
      },
      rules: {
        name: [
          {required: true, message: '请输入店铺名称', trigger: 'blur'},
          {min: 5, max: 15, message: '长度在 5到 15 个字符', trigger: 'blur'}
        ],
        address: [
          {required: true, message: '请输入店铺地址', trigger: 'blur'},
          {min: 5, max: 26, message: '长度在 5 到 26 个字符', trigger: 'blur'}
        ],
        logo: [
          {required: true, message: '请输入店铺logo', trigger: 'blur'},
          {min: 3, max: 55, message: '长度在 3 到 55 个字符', trigger: 'blur'}
        ],
        phone: [
          {required: true, message: '请输入商家电话', trigger: 'blur'},
          {min: 11, max: 11, message: '长度在 11 到 11 个字符', trigger: 'blur'}
        ],
        wechat: [
          {required: true, message: '请输入商家微信', trigger: 'blur'},
          {min: 6, max: 30, message: '长度在 3 到 30 个字符', trigger: 'blur'}
        ]
      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let url = 'http://localhost:9989/stores/add-new';
          console.log('url = ' + url);
          let formData = this.qs.stringify(this.ruleForm);
          console.log('formData = ' + formData);

          this.axios
              .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
              .post(url, formData).then((response) => {
            let responseBody = response.data;
            if (responseBody.state == 20000) {
              this.$message({
                message: '添加成功！',
                type: 'success'
              });
              this.resetForm(formName);
            } else {
              this.$message.error(responseBody.message);
            }
          });
        } else {
          alert('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
}
</script>

<style scoped>
</style>